from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from users.models import Dlmodel, Flmodel, Ddmodel

import re
import redis
import random

from utils.myjwt import generate_jwt

# Create your views here.


class Yzmview(APIView):
    def get(self,request):
        sjh = request.query_params.get('sjh')
        if not re.findall(r'^1[3-9]\d{9}$', sjh):
            return Response({'code': 400, "msg": '手机号错误'})
        data = random.randint(100000, 999999)

        cls = redis.Redis(host='localhost', port=6379, db=5)
        cls.set('sms_%s'%sjh, data, ex=300)
        return Response({'code': 200, "msg": '验证码发送成功', "data": data})


class Dlview(APIView):
    def post(self, request):
        sjh = request.data.get('sjh')
        yzm = request.data.get('yzm')
        print('xxxxxxxxxxxx', sjh,yzm)
        if not all([sjh, yzm]):
            return Response({'code': 400, "msg": '信息不全'})

        cls = redis.Redis(host='localhost', port=6379, db=5)
        rds_data = cls.get('sms_%s'%sjh)

        if rds_data.decode() != yzm:
            return Response({'code': 400, "msg": '验证码错误'})

        try:
            user = Dlmodel.objects.get(sjh=sjh)
            data_nm = {
                'sjh': sjh
            }
            token = generate_jwt(data_nm)
            return Response({'code': 200, "msg": '登录成功', 'token': token, "sjh": sjh, 'id': user.id})
        except Exception as e:
            print(e)
            user = Dlmodel.objects.create(sjh=sjh)
            data_nm = {
                'sjh': sjh
            }
            token = generate_jwt(data_nm)
            return Response({'code': 201, "msg": '注册成功', 'token': token, "sjh": sjh, 'id': user.id})


class Flview(APIView):
    def get(self, request):
        user = Flmodel.objects.all()
        ser = []
        for i in user:
            ser.append({
                'id': i.id,
                'fl': i.fl,
                'tp': i.tp,
            })
        return Response({'code': 200, "msg": "导航栏分类", "data": ser})


class Ddview(APIView):
    def get(self, request, id):
        user = Ddmodel.objects.filter(ss=id)
        ser = []
        for i in user:
            ser.append({
                'id': i.id,
                'mc': i.mc,
                'jg': i.jg,
                'rq': i.rq,
                'zt': i.zt,
                'tp': i.tp,
                'ss': i.ss_id,
            })
        return Response({'code': 200, 'msg': '订单', 'data': ser})

    def put(self, request, id):
        try:
            c1=Ddmodel.objects.get(id=id, zt=1)
            c1.zt=6
            c1.save()
            return Response({'code': 200, 'msg': '订单已取消'})
        except Exception as e:
            print(e)
            user = Ddmodel.objects.get(id=id)
            return Response({'code': 400, 'msg': "订单无法取消"})

    def delete(self, request, id):
        try:
            Ddmodel.objects.get(id=id).delete()
            return Response({'code': 200, 'msg': '删除订单成功'})
        except Exception as e:
            return Response({"code": 500, 'msg': '删除订单设备'})











